{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-10T07:37:01.546669Z",
     "start_time": "2025-10-10T07:36:40.480593Z"
    }
   },
   "source": [
    "import ssl\n",
    "import websocket\n",
    "import json\n",
    "\n",
    "# 全局计数器\n",
    "count = 5\n",
    "\n",
    "def on_message(ws, message):\n",
    "    global count\n",
    "    print(message)\n",
    "    count -= 1\n",
    "    # 接收了5次消息之后关闭websocket连接\n",
    "    if count == 0:\n",
    "        ws.close()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    ws = websocket.WebSocketApp(\n",
    "        \"wss://api.gemini.com/v1/marketdata/btcusd?top_of_book=true&offers=true\",\n",
    "        on_message=on_message)\n",
    "    ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "4a3045f1afde2a6"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
